﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.BLL;
using QldtSdh.DAL.Models;
using QldtSdh.GUI.Common;
using QldtSdh.GUI.Utils;
using QldtSdh.Utils;

namespace QldtSdh.GUI.DuLieu
{
    public partial class FormQuanLyDonVi_ChiTiet : BaseDialog
    {
        DonVi donViDaoTao;
        int countEntityAdded = 0;

        public event QldtSdh.GUI.Common.TListChangedEventHandler OnDonViListChanged;

        public FormQuanLyDonVi_ChiTiet(DonVi donViDaoTao = null)
        {
            InitializeComponent();
            this.donViDaoTao = donViDaoTao;
            InitInterface();
        }

        void InitInterface()
        {
            errorNotify.Clear();
            if (donViDaoTao != null)
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.EDIT, ENTITY_NAME.DONVIDAOTAO);
                txtMaDonVi.Text = donViDaoTao.MaDonVi;
                txtTenDonVi.Text = donViDaoTao.TenDonVi;
                txtTruongDonVi.Text = donViDaoTao.TruongDonVi;
                txtSoDienThoai.Text = donViDaoTao.SoDienThoai;
                txtGhiChu.Text = donViDaoTao.GhiChu;
                chkTiepTucBoSung.Checked = false;
                chkTiepTucBoSung.Visible = false;
            }
            else
            {
                this.Text = string.Format(TITLE_FORM_DETAIL.ADDNEW, ENTITY_NAME.DONVIDAOTAO);
                txtMaDonVi.Text = "";
                txtTenDonVi.Text = "";
                txtTruongDonVi.Text = "";
                txtSoDienThoai.Text = "";
                txtGhiChu.Text = "";
                chkTiepTucBoSung.Visible = true;
                chkTiepTucBoSung.Checked = true;
            }
            txtMaDonVi.Focus();
        }

        bool ValidateData()
        {
            errorNotify.Clear();
            if (txtMaDonVi.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtMaDonVi, string.Format(ERROR_CONSTANT_STRING.LOI_MARONG, ENTITY_NAME.DONVIDAOTAO));
                txtMaDonVi.Focus();
                return false;
            }
            if (txtTenDonVi.Text.IsNullOrEmpty())
            {
                errorNotify.SetError(txtTenDonVi, string.Format(ERROR_CONSTANT_STRING.LOI_TENRONG, ENTITY_NAME.DONVIDAOTAO));
                txtTenDonVi.Focus();
                return false;
            }
            return true;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            if (ValidateData())
            {
                if (donViDaoTao != null)
                {
                    #region Cập nhật thông tin đơn vị
                    donViDaoTao.MaDonVi = txtMaDonVi.Text;
                    donViDaoTao.TenDonVi = txtTenDonVi.Text;
                    donViDaoTao.TruongDonVi = txtTruongDonVi.Text;
                    donViDaoTao.SoDienThoai = txtSoDienThoai.Text;
                    donViDaoTao.GhiChu = txtGhiChu.Text;
                    var rs = BizDonViDaoTao.Update(donViDaoTao);
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {
                        this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        this.Dispose();
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtMaDonVi, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.DONVIDAOTAO));
                        txtMaDonVi.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTenDonVi, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.DONVIDAOTAO));
                        txtTenDonVi.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_CAPNHAT, ENTITY_NAME.DONVIDAOTAO));
                        return;
                    }
                    #endregion
                }
                else
                {
                    #region Thêm mới đơn vị
                    donViDaoTao = new DonVi();
                    donViDaoTao.MaDonVi = txtMaDonVi.Text;
                    donViDaoTao.TenDonVi = txtTenDonVi.Text;
                    donViDaoTao.TruongDonVi = txtTruongDonVi.Text;
                    donViDaoTao.SoDienThoai = txtSoDienThoai.Text;
                    donViDaoTao.GhiChu = txtGhiChu.Text;
                    var rs = BizDonViDaoTao.Add(donViDaoTao);
                    donViDaoTao = null;
                    if (rs == CODE_RESULT_RETURN.ThanhCong)
                    {

                        //Sau khi thêm thành công kiểm tra có cho nhập thêm hay không
                        countEntityAdded++;
                        if (OnDonViListChanged != null)
                            OnDonViListChanged(this, new EntityArgs(donViDaoTao));

                        if (chkTiepTucBoSung.Checked)
                        {
                            InitInterface();
                        }
                        else
                        {
                            this.DialogResult = System.Windows.Forms.DialogResult.OK;
                        }
                    }
                    else if (rs == CODE_RESULT_RETURN.MaTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtMaDonVi, string.Format(ERROR_CONSTANT_STRING.LOI_MATRUNG, ENTITY_NAME.DONVIDAOTAO));
                        txtMaDonVi.Focus();
                        return;
                    }
                    else if (rs == CODE_RESULT_RETURN.TenTrung)
                    {
                        errorNotify.Clear();
                        errorNotify.SetError(txtTenDonVi, string.Format(ERROR_CONSTANT_STRING.LOI_TENTRUNG, ENTITY_NAME.DONVIDAOTAO));
                        txtTenDonVi.Focus();
                        return;
                    }
                    else
                    {
                        DialogUtils.ShowError(string.Format(ERROR_CONSTANT_STRING.LOI_THEM, ENTITY_NAME.DONVIDAOTAO));
                        return;
                    }
                    #endregion
                }
            }
        }

        private void FormQuanLyDonVi_ChiTiet_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (countEntityAdded != 0)
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
